/*
 * Copyright (C) 拾光分享网 
 * All rights reserved, Designed By www.hiai.top.
 * Email：goodsking@163.com.
 */
package top.hiai.my.index;

import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.core.ActionKey;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import top.hiai.common.interceptor.FrontAuthInterceptor;
import top.hiai.common.model.Article;
import top.hiai.common.model.Message;
import top.hiai.common.model.User;
import top.hiai.config.BaseController;
import top.hiai.user.index.IndexUserService;

/**
* @Author www.hiai.top
* @Email  goodsking@163.com
* @Message 更多资源尽在www.hiai.top
* @Introduction 类的介绍:用户主页控制器
* 这里好友关注和取消关注控制
* 用户之间发送私信控制
*/
@Before(FrontAuthInterceptor.class)
public class IndexMyController extends BaseController {
	
	private IndexMyService indexMyService = new IndexMyService();
	
	/**
	 * 登录用户的个人主页
	 * 默认显示的是私信，后期改成好友的动态
	 */
	public void index(){
		//获取改用户的私信
		setAttr("userMessageList", indexMyService.userMessageListPage(getParaToInt("p", 1) ,limtCount, getLoginAccountId()));
		render("index.html");
		//renderJson();
	}
	/**
	 * 获取单个的私信对话
	 */
	public void message(){
		//Integer mid = getParaToInt();
        int friendId = getParaToInt();
        Page<Message> messagePage = indexMyService.userMessageDetailPage(getParaToInt("p", 1),limtCount, getLoginAccountId(), friendId);
        setAttr("messagePage", messagePage);
       
        User friend = new User().set("uid", friendId);
        setAttr("friend", friend);
        render("messageDetail.html");
	}
	/*
	 * 用于用户登录后查看私信给某一个用户对话的回复私信
	 */
	public void userMessageReply(){
		String replyContent = getPara("replyContent");
        
		if (StrKit.isBlank(replyContent)) {
            renderJson(Ret.fail().set("msg", "私信内容不能为空"));
            return ;
        }
		Ret ret = indexMyService.userMessageReply(getLoginAccountId(), getParaToInt("friendId"), replyContent);
        if (ret.isFail()) {
            renderJson(ret);
            return ;
        }
        ret.set("loginAccount", getLoginAccount());     // 放入 loginAccount 供 renderToString 使用
		
        // 用模板引擎生成 HTML 片段 replyItem
        String replyItem = renderToString("message_detail_reply.html", ret);
        
        ret.set("replyItem", replyItem);
        renderJson(ret);
	}
	/**
	 * 用户发送私信，先判断用户登录没，post提交
	 * errMsg = 0 没有登录
	 * errMsg = 1 登录了
	 */
	@ActionKey("/my/sendMessageVerify")
	@Clear(FrontAuthInterceptor.class)
	public void sendMessageVerify(){
		//判断用户是否登录
		boolean is_true_login = isLogin();
		if(is_true_login){
			renderJson(Ret.ok());
		}else{
			renderJson(Ret.fail().set("msg", "您没有登录,不给发送私信。"));// 定制返回值
		}	
	}
	
	/**
	 * 用户发送私信，保存发送的私信内容
	 * 用于用户主页上面的发送私信
	 */
	public void sendUserMessageAdd(){
		//获取登录用户ID，就是谁发的ID
		int accountId = getLoginAccountId();
		//获取发给谁的ID
		int friendId = getInt("uid");
		String content = get("message");
		Ret ret = indexMyService.sendUserMessageAdd(accountId,friendId,content);
		renderJson(ret);
	}
	/**
	 * 删除一条私信的内容
	 */
	public void deleteByMessageId(){
        Ret ret = indexMyService.deleteByMessageId(getLoginAccountId(), getParaToInt("messageId"));
        renderJson(ret);
	}
	/**
	 * 删除与这个用户的所有会话记录
	 */
	public void deleteByFriendIdAll(){
        Ret ret = indexMyService.deleteByFriendIdAll(getLoginAccountId(), getParaToInt("friendId"));
        renderJson(ret);
	}
	
	/**
	 * 查找用户
	 */
	public void sendFoundUser(){
		int uid = getParaToInt("uid");
		User user = IndexUserService.me.getDetailUser(uid);
		renderJson("result",user);
	}
	
	/**
	 * 关注好友
	 */
	@ActionKey("/my/friendAdd")
	@Clear(FrontAuthInterceptor.class)
	public void friendAdd(){
		if (notLogin()) {
			renderJson(Ret.fail().set("msg", "登录后才能添加好请先登录"));// 定制返回值
			return;
		}
		int accountId = getLoginAccountId();
		int uid = getParaToInt("friendId");
		Ret ret = indexMyService.myFriendAdd(accountId, uid);
		ret.set("friendRelation", IndexUserService.me.getFriendRelation(accountId, uid));
		renderJson(ret);
		//return null;
	}
	
	/**
	 * 取消关注好友
	 * @return
	 */
	@ActionKey("/my/friendDel")
	@Clear(FrontAuthInterceptor.class)
	public void friendDel(){
		int accountId = getLoginAccountId();
		int uid = getParaToInt("friendId");
		Ret ret = indexMyService.myFriendDel(accountId, uid);
		ret.set("friendRelation", IndexUserService.me.getFriendRelation(accountId, uid));
		renderJson(ret);
	}
	
	/**
	 * 显示该用户关注的好友的动态消息
	 */
	public void all(){
		Page<Article> articlePage = indexMyService.myFriendDynamicPage(getParaToInt("p", 1),limtCount,getLoginAccountId());
		setAttr("myFriendArticleList", articlePage);
		render("myFriendDynamic.html");
		//renderJson();
	}

	/** 用户下载历史 */
	public void download(){
		int uid = getLoginAccountId();
		setAttr("xiaList", indexMyService.paginateGetUserXia(getParaToInt("p", 1),limtCount, uid));
		render("/_view/my/download/my_download.html");
	}
}